package day08.edu;

import org.junit.Test;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Date;
import java.util.Scanner;

// Created:
public class ExceptionTest {
    @Test
    public void test1()
    {
        int[] arr = new int[10];
        System.out.println(arr[10]); // Array index out of bounds exception
    }

    @Test
    public void test2()
    {
        /*
        String str = "Hello";
        System.out.println(str.toString());


        str = null;
        System.out.println(str.toString());

        int[][] arr2 = new int[10][];
        System.out.println(arr2[0][0]);

         */

        Object obj = new String();
        Date date = (Date)obj; // class cast exception
        

    }

    // NumberFormatException
    @Test
    public void test3()
    {
        String str = "123";
        str = "abc";
        int i = Integer.parseInt(str); // NumberFormatException
        System.out.println(i);
    }

    // InputMismatchException
    @Test
    public void test4()
    {
        Scanner scan = new Scanner(System.in);
        int intScan = scan.nextInt();
        System.out.println(intScan); // InputMismatchException
    }

    // ArithmeticException
    @Test
    public void test5()
    {
        int num = 5;
        System.out.println(num / 0); // ArithmeticException
    }
    
    @Test
    public void test6() throws FileNotFoundException, IOException
    {
        File file = new File("D:\\helloword.txt");
        FileInputStream files = new FileInputStream(file); // FileNotFoundException

        int data = files.read(); // IOException
        while(data != -1)
        {
            System.out.println((char)data);
            data = files.read();
        }

        files.close();
    }
}
